from django.core.mail import send_mail
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer

from md import settings


def generate_verify_token(user_id):
    serializer = Serializer(settings.SECRET_KEY, 3600 * 2482)
    token = serializer.dumps({'user_id': user_id})
    token = token.decode()

    return token


def check_verify_token(token):
    serializer = Serializer(settings.SECRET_KEY, 3600 * 24 * 2)
    user_id_dict = serializer.loads(token)
    user_id = user_id_dict.get('user_id')

    return user_id


def send_verify_mail(subject, html_message, recipient_list):
    subject = subject  # 标题
    message = ""  # 内容
    from_email = "lc124666@163.com"  # 发送方地址
    recipient_list = recipient_list  # 接收方地址 列表
    try:
        print("发送邮件 的任务")
        send_mail(subject, message, from_email, recipient_list, html_message=html_message)
    except Exception as e:
        print("发送邮件失败", e)
